﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using SmartPhone.Un.Pro.DataContracts;
using SmartPhone.Un.Pro.BusinessLogic;
using SmartPhone.Un.Pro.DataAccess;
using SmartPhone.Un.Pro.Interface.BusinessLogic.Product;

namespace SmartPhone.Un.Pro.BusinessLogic.Product
{
    public class OMRepo : BaseRepository, IUnlockPhoneRepository
    {
        #region Constructor
        public OMRepo() { } 
        #endregion

        #region Properties
        private UNProEntities _entities;
        public UNProEntities Entities
        {
            get
            {
                if (_entities == null)
                {
                    _entities = new UNProEntities();
                }
                return _entities;

            }
        }

        private static IUnlockPhoneRepository _instance;
        public static IUnlockPhoneRepository Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new OMRepo();
                }
                return _instance;
            }
        } 
        #endregion
       
        #region login
        public AccountInfo Login(string userName, string password)
        {
            var userInfo = new AccountInfo();

            var obj = Entities.UserExtend.FirstOrDefault(p => p.UserName == userName &&
                                                               p.Password == password);

            if (IsNotNull(obj))
            {
                if (obj != null)
                    userInfo = new AccountInfo(userId: obj.UserID.GetGuid(), userName: obj.UserName.GetString(), password: obj.Password.GetString()
                                         );
            }
            return userInfo;
        }

        #endregion

        #region Brand

        public BrandInfo GetBrandInfoById(Guid id)
        {
            BrandInfo brandInfo = null;
            brandInfo = Entities.Brand.Where(pm => pm.BrandID == id).AsEnumerable()
                                    .Select(pm => new BrandInfo()
                                    {
                                        BrandId = pm.BrandID,
                                        BrandName = pm.BrandName,
                                        BrandOrder = pm.BrandOrder.GetInteger()
                                    }).FirstOrDefault();
            return brandInfo;
        }

        public List<BrandInfo> GetAllBranch()
        {
            List<BrandInfo> brandInfo = null;

            var sbranchInfo = Entities.Brand.AsEnumerable()
                                          .Select(pm => new BrandInfo()
                                          {
                                              BrandId = pm.BrandID,
                                              BrandName = pm.BrandName.GetString(),
                                              BrandOrder = pm.BrandOrder.GetInteger()
                                          })
                                          .ToList();
            return brandInfo;
        }


        #endregion

        #region Insert
        #region brand

        public Guid InsertBrand(string brandName, int brandOrder = 1)
        {
            

            //get last brandOrder
            if (brandOrder > 1)
            {
                var objBrand = Entities.Brand.OrderByDescending(p => p.BrandOrder).FirstOrDefault();
                if (objBrand != null)
                {
                    brandOrder = objBrand.BrandOrder.GetInteger() + 1;
                }
            }

            //create new object Brand
            var brand = new Brand()
                {
                    BrandID = Guid.NewGuid(),
                    BrandOrder = brandOrder,
                    BrandName = brandName
                };

            //add brand object
            Entities.Brand.AddObject(brand);

            //save change
            var result = Entities.SaveChanges();
            return result > 0 ? brand.BrandID : Guid.Empty;
        }

        #endregion

        #endregion

        #region Model

        public ModelInfo GetModelInfoById(Guid id)
        {
            ModelInfo modelInfo = null;
            modelInfo = Entities.Model.Where(pm => pm.ModelID == id).AsEnumerable()
                                    .Select(pm => new ModelInfo()
                                    {
                                        BrandInfo = GetBrandInfoById(pm.BrandID.GetGuidNullable()),
                                        ModelId = pm.ModelID,
                                        ModelName = pm.ModelName,
                                        ModelOrder = pm.ModelOrder.GetInteger()
                                    }).FirstOrDefault();
            return modelInfo;
        }
        #endregion

        #region Carrier
        public CarrierInfo GetCarrierInfoById(Guid id)
        {
            CarrierInfo carrierInfo = null;
            carrierInfo = Entities.Carrier.Where(pm => pm.CarrierID == id).AsEnumerable()
                                    .Select(pm => new CarrierInfo()
                                    {
                                        CarrierId = pm.CarrierID,
                                        CarrierName = pm.CarrierName,
                                        CarrierOrder = pm.CarrierOrder.GetInteger()
                                    }).FirstOrDefault();
            return carrierInfo;
        }
        #endregion

        #region Product
        public List<ProductInfo> GetAllProduct()
        {
            var productInfo = new List<ProductInfo>();

            var sbranchInfo = Entities.Product.AsEnumerable()
                                          .Select(pm => new ProductInfo()
                                          {
                                              CarrierInfo = GetCarrierInfoById(pm.CarrierID.GetGuidNullable()),
                                              CreatedDate = pm.CreatedDate.GetDate(),
                                              ModelInfo = GetModelInfoById(pm.ModelID.GetGuidNullable()),
                                              PriceCurrent = pm.PriceCurrent,
                                              ProductId = pm.ProductID,
                                              ProductImageId = pm.Product_ImageID,
                                              ProductOrder = pm.ProductOrder.GetInteger(),
                                              QuantityCurrent = pm.QuantityCurrent.GetInteger(),
                                              UpdatedDate = pm.UpdatedDate.GetDate()
                                          })
                                          .ToList();
            return productInfo;
        }
        #endregion

        #region Order

        #endregion





    }//close class
}//close namespace
